package Demo1;

import java.util.Date;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-09-13
 * Time: 8:27
 */
public class Test {
    static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    /**
     * @param root 代表节点
     * @return 返回该节点左树和右树各自最大高度的和
     */
    public static int maxSum = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        len(root);
        return maxSum;
    }

    public int len(TreeNode root){
        if(root == null) return 0;
        int left = len(root.left);
        int right = len(root.right);
        if(maxSum < left + right) maxSum = left + right;
        return Math.max(left,right) + 1;
    }
    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        node1.left = node2;
        Test t = new Test();
        System.out.println(t.diameterOfBinaryTree(node1));
    }
}
